﻿using Simple.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.Utils.Extensions
 * 接口名称 ByteExtension
 * 开发人员：zdm
 * 创建时间：2023/11/23 9:43:27
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Utils.Extensions
{
	/// <summary>
	/// 
	/// </summary>
	public static class ByteExtension
	{
		/// <summary>
		/// 字符串id转换为byte 数组
		/// </summary>
		/// <param name="Ip"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		/// <exception cref="Exception"></exception>
		public static byte[] IPToBytes(this string Ip)
		{
			if (string.IsNullOrEmpty(Ip))
			{
				throw new ArgumentNullException(nameof(Ip));
			}

			if (!Ip.Contains("."))
			{
				throw new Exception("字符串ip转换成byte [] 出现异常,请检查ip地址!");
			}
			byte[] ipBuf = new byte[4];
			string[] arr = Ip.Split('.');
			ipBuf[0] = (byte)(int.Parse(arr[0]) & 0xFF);
			ipBuf[1] = (byte)(int.Parse(arr[1]) & 0xFF);
			ipBuf[2] = (byte)(int.Parse(arr[2]) & 0xFF);
			ipBuf[3] = (byte)(int.Parse(arr[3]) & 0xFF);
			return ipBuf;
		}
		/// <summary>
		/// IPaddress 转换为byte[]
		/// </summary>
		/// <param name="address"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		public static byte[] IPToBytes(this IPAddress address)
		{
			if (SimpleCheck.IsNull(address))
			{
				throw new ArgumentNullException(nameof(address));
			}
			return address.ToString().IPToBytes();
		}
		/// <summary>
		/// 时间  to bytes
		/// </summary>
		/// <param name="datetime"></param>
		/// <returns></returns>
		public static byte[] CurrentTimeToBytes(this DateTime datetime)
		{
			string time = datetime.ToString("HHmmss");
			byte[] timeBuf = Encoding.UTF8.GetBytes(time);
			return timeBuf;
		}
		/// <summary>
		/// 日期 to bytes
		/// </summary>
		/// <param name="datetime"></param>
		/// <returns></returns>
		public static byte[] DateToBytes(this DateTime datetime)
		{
			string date = datetime.ToString("yyyyMMdd");
			date = date.Substring(2);
			byte[] dateBuf = Encoding.UTF8.GetBytes(date);
			return dateBuf;
		}
		/// <summary>
		/// int to bytes[]
		/// </summary>
		/// <param name="Port"></param>
		/// <returns></returns>
		public static byte[] IntToBytes(this int Port)
		{
			byte[] hex = BitConverter.GetBytes(Port);
			Array.Reverse(hex);
			return hex;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="data"></param>
		/// <param name="checkIndex"></param>
		/// <returns></returns>
		public static int CalculateChecksum(this byte[] data, int checkIndex)
		{
			int checksum = 0;
			for (int i = 0; i < checkIndex; i++)
			{
				checksum += data[i];
			}
			checksum &= 0xff;
			return checksum;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public static byte[] NumToByte(this int value, int length)
		{
			byte[] hex = BitConverter.GetBytes(value);

			byte[] temp = new byte[length];
			Array.Copy(hex, 0, temp, 0, hex.Length <= length ? hex.Length : length);
			Array.Reverse(temp);
			return temp;
		}
	}
}
